package com.sys.gradepaysys.config;

import org.apache.ibatis.plugin.Interceptor;
import org.apache.ibatis.session.Configuration;
import org.mybatis.spring.SqlSessionFactoryBean;
import org.mybatis.spring.mapper.MapperScannerConfigurer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;

import javax.sql.DataSource;

public class MybatisConfig {
    @Autowired
    DataSource dataSource;

    //SqlsessionFactoryBean负责给我们广产生sqlsession知道数据源，在创建这个对像的时候指定你的数据源
    @Bean("sessionFactory")
    public SqlSessionFactoryBean sqlsessionFactoryBean(DataSource dataSource) {
        SqlSessionFactoryBean sqlSessionFactoryBean= new SqlSessionFactoryBean();
        //引入数据源配置
        sqlSessionFactoryBean.setDataSource(dataSource);
        Configuration config =new Configuration();
        //开启驼峰命名[mybatis配置文件操作，settings标签配置]
        config.setMapUnderscoreToCamelCase(true);
        sqlSessionFactoryBean.setTypeAliasesPackage("com.sys.gradepaysys.model");
        return sqlSessionFactoryBean;
    }

    @Bean("mapperScannerConfigurer")
    public MapperScannerConfigurer mapperScannerConfigurer() {
        MapperScannerConfigurer scannerConfigurer = new MapperScannerConfigurer();
        scannerConfigurer.setBasePackage("com.xxx.mapper");
        scannerConfigurer.setSqlSessionFactoryBeanName("sessionFactory");
        return scannerConfigurer;
    }
}
